package com.wzu.rjgc_ks.controller;

import com.wzu.rjgc_ks.dao.*;
import com.wzu.rjgc_ks.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author 李敏惠 16219111214
 * @date 2019/9/3 11:21
 */




@Controller
@RequestMapping("/stu")
public class StuController {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ReaumeMapper reaumeMapper;
    @Autowired
    IntentMapper intentMapper;
    @Autowired
    WorkMapper workMapper;
    @Autowired
    JobMapper jobMapper;
    @Autowired
    CompanyMapper companyMapper;

    Integer stuId;
    Integer reaId;
    Integer intId;
    Integer worId;

    @RequestMapping("/main")
    public String mainIndex(){
     return "student/studentMain";
    }

    //获取基本信息
    @RequestMapping("/stu")
//    public ModelAndView Stu_Info(HttpServletRequest request){
//        ModelAndView mv = new ModelAndView();
//        request.getSession().setAttribute("stuId",9);
//        Integer stuId =(Integer) request.getSession().getAttribute("stuId");
//        Student student = studentMapper.selectByPrimaryKey(stuId);
//        mv.addObject("student",student);
//        mv.setViewName("student/studentMain");
//        return mv;
//    }
    @ResponseBody
    public HashMap<String,Object> stuInfo(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        stuId =(Integer) request.getSession().getAttribute("stuId");
        reaId = studentMapper.selectResId(stuId);
        Student student = studentMapper.selectByPrimaryKey(stuId);
        map.put("student",student);
        return map;
    }

    //更新基本信息
    @RequestMapping(value= "/basicInfo", method = RequestMethod.PUT)
    @ResponseBody
    public void updateStuInfo(Student student){
        System.out.println(student.getStuJobState());
        student.setStuId(stuId);
        studentMapper.updateByPrimaryKey(student);
    }

    //获取我的简历
    @RequestMapping("/resume")
    @ResponseBody
    public HashMap<String,Object> stuResume(){
        HashMap<String,Object> map = new HashMap<>();
        reaId = studentMapper.selectResId(stuId);
        if(reaId==null){
            map.put("resume",null);
        }else{
            Reaume resume = reaumeMapper.selectByPrimaryKey(reaId);
            map.put("resume",resume);
        }
        return map;
    }

    //更新我的简历
    @RequestMapping(value= "/resume", method = RequestMethod.PUT)
    public String updateStuRea(Reaume resume){
        if(reaId==null){
            reaumeMapper.insert(resume);
            reaId = resume.getReaId();
            studentMapper.updateRea(stuId,reaId);
        }else{
            resume.setReaId(reaId);
            reaumeMapper.updateByPrimaryKey(resume);
        }
        return "redirect:/stu/resume";
    }

    //删除我的简历
    @RequestMapping(value="/resume",method = RequestMethod.DELETE)
    public String deleteStudentRea(){
        Integer t = null;
        studentMapper.updateRea(stuId,t);
        reaumeMapper.deleteByPrimaryKey(reaId);
        reaId = null;
        return "redirect:/stu/resume";
    }

    //获取求职意向
    @RequestMapping("/intent")
    @ResponseBody
    public HashMap<String,Object> stuIntent(){
        HashMap<String,Object> map = new HashMap<>();
        intId = studentMapper.selectIntId(stuId);
        if(intId==null){
            map.put("intent",null);
        }else{
            Intent intent = intentMapper.selectByPrimaryKey(intId);
            map.put("intent",intent);
        }
        return map;
    }

    //更新求职意向
    @RequestMapping(value= "/intent", method = RequestMethod.PUT)
    public String updateStuInt(Intent intent){
        if(intId==null){
            intentMapper.insert(intent);
            intId = intentMapper.selectLastInsert();
            System.out.println(intId);
            studentMapper.updateInt(stuId,intId);
        }else{
            intent.setIntId(intId);
            intentMapper.updateByPrimaryKey(intent);
        }
        return "redirect:/stu/intent";
    }

    //删除求职意向
    @RequestMapping(value="/intent",method = RequestMethod.DELETE)
    public String deleteStudentInt(){
        Integer t = null;
        studentMapper.updateInt(stuId,t);
        intentMapper.deleteByPrimaryKey(intId);
        intId = null;
        return "redirect:/stu/intent";
    }

    //获取就业情况
    @RequestMapping("/work")
    @ResponseBody
    public HashMap<String,Object> stuWork(){
        HashMap<String,Object> map = new HashMap<>();
        worId = studentMapper.selectWorId(stuId);
        if(worId==null){
            map.put("work",null);
        }else{
            Work work = workMapper.selectByPrimaryKey(worId);
            map.put("work",work);
        }
        return map;
    }

    //更新就业情况
    @RequestMapping(value= "/work", method = RequestMethod.PUT)
    @ResponseBody
    public Work updateStuWor(Work work){
        if(worId==null){
            System.out.println(work.getWorTime());
            workMapper.insert(work);
            worId = workMapper.selectLastInsert();
            System.out.println(worId);
            studentMapper.updateWor(stuId,worId);
            studentMapper.updateWorState(stuId);
        }else{
            work.setWorId(worId);
            workMapper.updateByPrimaryKey(work);
        }
        return work;
    }

    //删除就业情况
    @RequestMapping(value="/work",method = RequestMethod.DELETE)
    @ResponseBody
    public void deleteStudentWor(){
        Integer t = null;
        studentMapper.updateWor(stuId,t);
        workMapper.deleteByPrimaryKey(worId);
        worId = null;
    }

    //获取我的收藏
    @RequestMapping(value = "/collectJob", method = RequestMethod.GET)
    @ResponseBody
    public List<Job> findAll() {
        return jobMapper.selectStuCollect(stuId);
    }

    //删除收藏职位


    //获取被收藏情况
    @RequestMapping(value = "/beCollected", method = RequestMethod.GET)
    @ResponseBody
    public List<Company> findBeCollected() {
        return companyMapper.selectBeCollectd(reaId);
    }



    @Value("${file.postfix}")
    String POSTFIX;
    @Value("${file.save.path}")

    String SAVE_PATH;
    //上传图片
    @PostMapping(value = "/upload")
    @ResponseBody
    public com.springboot.pojo.Msg upload(@RequestParam(value = "photoFile") MultipartFile file, HttpServletRequest request) throws IOException {
        if (file != null) {
            //获取文件后缀
            String postfix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            //判断文件类型是否合法
            if (!POSTFIX.contains(postfix)) {
                return com.springboot.pojo.Msg.fail("不支持的文件类型");
            }

            System.out.println(file.getOriginalFilename());


            UUID uuid = UUID.randomUUID();
            String oFileName = uuid.toString().replace("-", "") + postfix;
            String fileName = SAVE_PATH + oFileName;
            file.transferTo(new File(fileName));
            System.out.println(oFileName);
            workMapper.updateProof(worId,fileName);
            return com.springboot.pojo.Msg.success(oFileName);

        }
        return com.springboot.pojo.Msg.fail("文件保存出错");
    }

}
